/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is.bean.stateless.xdumper;

import is.xdumper.type.Dump;
import is.xdumper.type.ValidatorResultContainer;
import is.xdumper.type.Entity;
import is.bean.entity.NamedEntityRemoteAccess;
import is.bean.entity.department.Department;
import is.bean.entity.department.DepartmentBeanRemoteHome;
import is.bean.entity.employee.Employee;
import is.bean.entity.employee.EmployeeBeanRemoteHome;
import is.bean.entity.region.Region;
import is.bean.entity.region.RegionBeanRemoteHome;
import is.xdumper.*;
import is.xdumper.xml.XdumperXmlMarshaller;
import is.xdumper.xml.XdumperXmlReader;
import java.io.*;
import java.rmi.RemoteException;
import java.util.Collection;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author aspr0
 */
public class XdumperBean implements SessionBean
{
    private SessionContext context;
    private DepartmentBeanRemoteHome departmentHome;
    private EmployeeBeanRemoteHome employeeHome;
    private RegionBeanRemoteHome regionHome;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    @Override
    public void setSessionContext(SessionContext aContext)
    {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    @Override
    public void ejbActivate()
    {
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    @Override
    public void ejbPassivate()
    {
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    @Override
    public void ejbRemove()
    {
        departmentHome = null;
        employeeHome = null;
        regionHome = null;
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() throws NamingException
    {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
        Context initial = new InitialContext();

        Object depRef = initial.lookup("ejb/DepartmentBean");
        Object empRef = initial.lookup("ejb/EmployeeBean");
        Object regRef = initial.lookup("ejb/RegionBean");

        departmentHome =
                (DepartmentBeanRemoteHome) PortableRemoteObject.narrow(depRef, DepartmentBeanRemoteHome.class);
        employeeHome =
                (EmployeeBeanRemoteHome) PortableRemoteObject.narrow(empRef, EmployeeBeanRemoteHome.class);
        regionHome =
                (RegionBeanRemoteHome) PortableRemoteObject.narrow(regRef, RegionBeanRemoteHome.class);
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")

    public void doExport(Dump index, File xml) throws FinderException, RemoteException, IOException
    {
        XdumperDumpCollector collector = new XdumperDumpCollector();
        collector.setDepartmentHome(departmentHome);
        collector.setEmployeeHome(employeeHome);
        collector.setRegionHome(regionHome);
        collector.setDump(index);

        collector.doCollect();

        XdumperXmlMarshaller.marshalDump(collector.getDump(), xml);
    }

    @Deprecated
    public Collection<NamedEntityRemoteAccess> listItemsExport(Entity entity) throws RemoteException
    {
        Collection<NamedEntityRemoteAccess> remotes = null;

        switch (entity)
        {
            case EMPLOYEE:
//                remotes = employeeHome.findByExample(new Employee());
                break;

            case DEPARTMENT:
//                remotes = departmentHome.findByExample(new Department());
                break;

            case REGION:
//                remotes = regionHome.findByExample(new Region());
                break;
        }

        return remotes;
    }

    public void doImport(Dump dump) throws FinderException, RemoteException, CreateException
    {
        XdumperImporter importer = new XdumperImporter();
        importer.setDepartmentHome(departmentHome);
        importer.setEmployeeHome(employeeHome);
        importer.setRegionHome(regionHome);
        importer.setDump(dump);

        importer.doImport();
    }

    /* passing stream to bean caused serialization error, changed to string */
    public ValidatorResultContainer validateImport(String dumpXml) throws ParserConfigurationException,
                                                                          SAXException, IOException,
                                                                          FinderException, NamingException,
                                                                          RemoteException,
                                                                          XdumperNoPathException
    {
        XdumperImportValidator validator = new XdumperImportValidator();
        validator.setDepartmentHome(departmentHome);
        validator.setEmployeeHome(employeeHome);
        validator.setRegionHome(regionHome);
        validator.setDump(XdumperXmlReader.read(dumpXml));

        validator.doValidate();

        return validator.getResultContainer();
    }
}
